(function($){    
    ujs.define('file', 'ujs.browser', {
        /**
         * 判断文件是否一张图片。  
         */
        isImage : function(blob){
            return blob.type && blob.type.indexOf('image/') === 0;
        },
        /**
         * 生成文件对象相应的图片对象.  
         */
        toImage : function(blob, cb){
            var image = new Image();        
            image.onload = function() {
                cb(this);
            };
            image.src = URL.createObjectURL(blob);
            return image;
        },
        isGif : function(blob){
            return blob.type && blob.type.indexOf('image/gif') === 0;
        },
        /**
         * 生成文件对象的本地Web路径.  
         */
        toLocalURL : function(blob){
            return URL.createObjectURL(blob);
        },
        /**
         * 批量下载文件. 
         */
        download : function() {        
            Object.toArray(arguments).linear().forEach(function( fileUrl ){
                $('<iframe class="downloadIframe h" src="{0}"></iframe>'.format( fileUrl )).appendTo(document.body);
            });           
            /*
                                    若使用ifrme下载文件，而非预览文件，则不会触发load事件。所以下面的代码不起作用：
                            $downloadIframe.load(function(){
                                $(this).remove();
                            });        
                                    缺点：每下载一个文件，将会生成一个冗余的iframe标签.
            */           
            /*
                                      参考:
                onload is only called when a document is loaded into an iframe.
                A Save operation does not load a document into the frame;
                the old document is left in place and the save-as dialogue is popped-up.
            
                You cannot detect that a file download has been accepted or completed from JavaScript alone.
                If you really need to detect that,
                the closest you would be able to get would be to have to have the server-side store the progress of the download by a unique ID,
                and provide an AJAX interface for the client-side script to query whether the server had finished sending the file yet.
            */    
        },
        downloadFromLink : function(fileUrl, defFileName) {  
            var fileName = ujs.str.getFileNameOfUrl(fileUrl) || defFileName;
            bubble.prompt('Click ok to download:', fileName, function(newFileName){
                if(newFileName){
                    $('<a href="{0}" download="{1}">Download</a>'.format(fileUrl, newFileName)).clickLink();
                }
            });
        },
        /**
         * 打开选择文件对话框.  
         */
        openFileSelector : (function(){
            var $fileField = $('<input type="file" style="display:none;" multiple />');
            $fileField.bind('click', function(){
                //清空$fileField中的文件,以便于每次选择文件都触发change事件.
                $(this).val('');
            });
            return function(cfg, cb){
                $fileField.unbind('change').bind('change', function() {
                    if(Type.isFunction(cb)){
                        cb(Array.toArray($fileField[0].files));
                    }
                });
                $fileField.trigger('click');
            };
        })()
    });   
    
})($);